﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace MyList_Enumerator
{
    class MyList<T> : ISupperList<T>, IEnumerable<T> where T : IComparable
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="items"></param>
        public MyList(T[] items)
        {
            this.m_elements = items;
            m_lenght = items.Length;
        }

        #region ISupperList implementation

        /// <summary>
        /// Add item of the T type from the list
        /// </summary>
        /// <param name="item"></param>
        public void Add(T item)
        {
            T[] temp = new T[m_lenght + 1];
            int i = 0;

            while (i < temp.Length)
            {
                temp[i] = m_elements[i++];
            }

            temp[i] = item;
            m_lenght++;
        }

        /// <summary>
        /// Delete item of the T type from the list
        /// </summary>
        /// <param name="item"></param>
        public void Delete(T item)
        {
            T[] temp = new T[m_lenght - 1];
            int i = 0;

            while (i < temp.Length)
            {
                temp[i] = (m_elements[i].CompareTo(item) == 1) ? m_elements[i + 1] : m_elements[i];
                i++;
            }

            m_lenght--;
        }

        /// <summary>
        /// Get count of the list
        /// </summary>
        /// <returns></returns>
        public int Count()
        {
            return m_lenght;
        }

        /// <summary>
        /// Get element on the possition 'pos'
        /// </summary>
        /// <param name="pos"></param>
        /// <returns></returns>
        public T GetPos(int pos)
        {
            T result;
           
            if(pos > m_lenght && pos < 0)
            {
                throw new ArgumentOutOfRangeException();
            }
            else
            {
                result = m_elements[pos];
            }

            return result;
        }

        #endregion

        #region IEnumerable implementation

        public IEnumerator<T> GetEnumerator()
        {
           // return new MyListTEnum_insert<T>(elements);
            return new MyListTEnum_insert<T>(this);
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return (IEnumerator<T>)GetEnumerator();
        }

        #endregion

        #region Private members

        private int m_lenght = 0;
        private T[] m_elements;

        #endregion

        #region Inserted 'MyListTEnum_insert<T>' class

        class MyListTEnum_insert<T> : IEnumerator<T> where T : IComparable
        {
            /// <summary>
            /// Constructor of the file
            /// </summary>
            /// <param name="myList"></param>
            public MyListTEnum_insert(MyList<T> myList)
            {
                // TODO: Complete member initialization
                this.myList = myList;
                position = -1;
                positionStart = 0;
                positionEnd = myList.m_lenght;
                index = 0;
            }

            #region IEnumerator<T> implementation
            
            public T Current
            {
                get { return current; }
            }

            public void Dispose()
            {
            }

            object IEnumerator.Current
            {
                get
                {
                    try
                    {
                        current = myList.m_elements[position];
                        return current;
                    }
                    catch (IndexOutOfRangeException)
                    {
                        throw new InvalidOperationException();
                    }
                }
            }

            public bool MoveNext()
            {
                if (index++ % 2 == 0)
                {
                    position = positionStart++;
                }
                else
                {
                    position = positionEnd--;
                }

                return (positionEnd >= positionStart);
            }

            public void Reset()
            {
                position = -1;
                positionStart = -1;
                positionEnd = myList.m_lenght;
                index = 0;
            }

            #endregion

            #region Private fields

            private T current;
            private int position;
            private int positionStart;
            private int positionEnd;
            private int index;
            private MyList<T> myList;

            #endregion
        }
        #endregion
    }
}
